package com.qdlc.p2p.manage.action;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Action;

import com.opensymphony.xwork2.ModelDriven;
import com.qdlc.p2p.biz.service.CustomerService;
import com.qdlc.p2p.biz.web.action.BaseAction;
import com.qdlc.p2p.common.util.DateUtil;
import com.qdlc.p2p.common.util.MessageUtil;
import com.qdlc.p2p.dal.common.model.jpa.PageDataList;
import com.qdlc.p2p.dal.dto.Customer;
import com.qdlc.p2p.dal.model.CustomerModel;
/**
 * 
 * 客服 管理action
 * @author zlhx
 * @version 2.0
 * @date 2015年8月3日 下午4:52:59
 */
public class ManageCustomerAction extends BaseAction<CustomerModel> implements ModelDriven<CustomerModel> {
	@Resource
	private CustomerService customerService;
	
	private Map<String, Object> data;
	/**
	 * 客服管理
	 * @return
	 */
	@Action(value = "/modules/user/customer/manager")
	public String manager(){
		return "manager";
	}
	/**
	 * 获取客服列表	
	 * @throws IOException
	 */
	@Action(value = "/modules/user/customer/list")
	public void list() throws IOException{
		data = new HashMap<String, Object>();
		PageDataList<CustomerModel> pagaDataList = customerService.pList(model);
		int total = pagaDataList.getPage().getTotal();
		data.put("total", total);
		data.put("rows", pagaDataList.getList());
		printJson(getStringOfJpaObj(data));
	}

	/**
	 * 添加
	 * @return
	 */
	@Action(value = "/modules/user/customer/append")
	public String append(){
		return "append";
	}
	
	/**
	 * 添加
	 * @throws Exception
	 */
	@Action("/modules/user/customer/doAppend")
	public void doAppend() throws Exception {
		data = new HashMap<String, Object>();
		Customer customer = model.prototype();
		customer.setAddTime(DateUtil.getNow());
		customerService.save(customer);
		data.put("result", "true");
		data.put("msg", "添加成功");
		printJson(getStringOfJpaObj(data));
	}
	
	/**
	 * 修改
	 * @return
	 */
	@Action("/modules/user/customer/modify")
	public String modify() {
		Customer customer = customerService.find(model.getId());
		request.setAttribute("customer", customer);
		return "modify";
	}
	
	
	/**
	 * 修改
	 * @return
	 */
	@Action("/modules/user/customer/doModify")
	public void doModify() throws Exception {
		data = new HashMap<String, Object>();
		Customer c = customerService.find(model.getId());
		Customer customer = model.prototype();
		customer.setIsDelete(c.getIsDelete());
		customer.setAddTime(c.getAddTime());
		customerService.modify(customer);
		data.put("result", "true");
		data.put("msg", MessageUtil.getMessage("MS102002001"));
		printJson(getStringOfJpaObj(data));
	}
	
	
	/**
	 * 禁用
	 * @return
	 */
	@Action("/modules/user/customer/disable")
	public void disable() throws Exception {
		customerService.modify(model.getId(), CustomerModel.DISABLE);
		printWebSuccess();
	}
	
	/**
	 * 启用
	 * @return
	 */
	@Action("/modules/user/customer/enable")
	public void enable() throws Exception {
		customerService.modify(model.getId(), CustomerModel.ENABLE);
		printWebSuccess();
	}
	
	
	/**
	 * 删除
	 * @return
	 */
	@Action("/modules/user/customer/delete")
	public void delete() throws Exception {
		customerService.delete(model.getId(), CustomerModel.ISDELETE);
		printWebSuccess();
	}
	
	
	public Map<String, Object> getData() {
		return data;
	}

	public void setData(Map<String, Object> data) {
		this.data = data;
	}
	
}
